package com.yhny.lykj.yuhannongye.app;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;


import com.yhny.lykj.yuhannongye.R;

import java.util.List;

/**
 */
public abstract class BaseTabPagerActivityNoNet extends BaseActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private int tabPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());

        onSetContentViewAfter();

        initTabPagerView();

        preloadData();
    }

    protected void onSetContentViewAfter() {
    }

    protected void preloadData() {
    }

    @LayoutRes
    protected int getLayoutResource() {
        return R.layout.activity_tab_pager;
    }

    protected abstract List<Fragment> getFragmentList();

    protected abstract int setTabPage();

    protected abstract List<String> getTitleList();

    private void initTabPagerView() {
        List<Fragment> fragmentList = getFragmentList();
        List<String> titleList = getTitleList();

        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList, titleList);

        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(tabFragmentAdapter);
        viewPager.setOffscreenPageLimit(3);

        tabLayout = (TabLayout) findViewById(R.id.tab);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (String title : titleList) {
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(tabFragmentAdapter);

        TabLayout.Tab tab = tabLayout.getTabAt(setTabPage());
        if (tab != null) {
            tab.select();
        }
    }


    protected int selectPage() {
        return 0;
    }

    protected void resizeViewInViewPager(ViewPager viewPager) {
    }
}
